package org.example.springmvc_demo7.controller;

import jakarta.servlet.http.HttpSession;
import org.example.springmvc_demo7.constant.Constants;
import org.example.springmvc_demo7.model.Result;
import org.example.springmvc_demo7.model.UserInfo;
import org.example.springmvc_demo7.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping("/login")
    public Result login(String name, String password, HttpSession session){
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return Result.Fail("用户名或密码为空");
        }

        UserInfo userInfo = userService.getUserInfoByName(name);

        if(userInfo == null){
            return Result.Fail("该用户不存在");
        }

        if(!password.equals(userInfo.getPassword())){
            return Result.Fail("密码错误");
        }

        session.setAttribute(Constants.USER_SESSION_KEY,userInfo);
        return Result.success("");


    }


}
